/* -----------------------------------------------------------------------------
*  CNumeric
*  Author: Antonio Javier Gallego Sánchez (ajgallego AT gmail DOT com)
*  License: GNU Public License
* ----------------------------------------------------------------------------*/

#include "CNumeric.hpp"

#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <time.h>
#include <limits>
#include <string>

#include "CString.hpp"
#include "asrtbas.h"
using namespace std;


//-----------------------------------------------------------------------
//
static bool prv_random_generation_is_initialized = false;

int CNumeric :: getRandomNumber( int min, int max )
{
    if (min >= max)
        return min;

    if( ! prv_random_generation_is_initialized )
    {
        srand ( time(NULL) );
        prv_random_generation_is_initialized = true;
    }

    return ( (rand() % (max - min + 1)) + min);
}

//-----------------------------------------------------------------------
//
const char* CNumeric :: dec_to_hex( int value )
{
    stringstream out;
    out.setf ( ios_base::uppercase );
    out << hex << value;

    return out.str().c_str();
}

//-----------------------------------------------------------------------
//
int CNumeric :: hex_to_dec( const char* value )
{
    int return_num;

    std::istringstream iss( value );
    iss >> noskipws >> hex >> return_num;

    assert( iss.eof() && !iss.fail() );

    return return_num;
}

//-----------------------------------------------------------------------
//
int CNumeric :: dec_to_oct( int value )
{
    stringstream out;
    out << oct << value;

    int oct;
    CString::stringToOct( out.str().c_str(), &oct );

    return oct;
}

//-----------------------------------------------------------------------
//
const char* CNumeric :: dec_to_bin( int decimalNum )
{
    assert( false &&
            "not tested" );

    string strBinaryNum;

    for ( int power = 1; decimalNum > 0; power *= 10, decimalNum >>= 1 )
        strBinaryNum += ((decimalNum)&1) * power;

    return strBinaryNum.c_str();
}

//-------------------------------------------------------------
//
int CNumeric :: GetMaxInt()
{
    return std::numeric_limits<int>::max();
}

//-------------------------------------------------------------
//
unsigned int CNumeric :: GetMaxUInt()
{
    return std::numeric_limits<unsigned int>::max();
}

//-------------------------------------------------------------
//
float CNumeric :: GetMaxFloat()
{
    return std::numeric_limits<float>::max();
}

//-------------------------------------------------------------
//
double CNumeric :: GetMaxDouble()
{
    return std::numeric_limits<double>::max();
}

/**
*/